package com.topscomm.gpm.xxljob;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.MessageException;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.gpm.pojo.GpmProjectSuperviseEntity;
import com.topscomm.gpm.service.impl.GpmProjectSuperviseService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@JobHandler(value = "projectSuperviseCheckJob")
@Component
public class ProjectSuperviseCheckJob extends IJobHandler {
    /**
     * @author zhangjunyao
     * @date 2021/12/21
     * @description:  检查更新督办状态，超过3天的置为超期
     * @modify:
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnT<String> execute(String param) throws Exception {
        try {
            GpmProjectSuperviseService gpmProjectSuperviseService = (GpmProjectSuperviseService) SpringUtil.getBean("gpmProjectSuperviseService");
            StringBufferProxy sql = new StringBufferProxy();
            // 查询处于审批中状态的督办单
            sql.appendSingle(" state = '1' and {0} != '1' and to_char({1}, 'yyyy-MM-dd') <= to_char(SYSDATE, 'yyyy-MM-dd')",
                    GpmProjectSuperviseEntity.FieldSupervisestate, GpmProjectSuperviseEntity.FieldSuperviseperiod);
            List<GpmProjectSuperviseEntity> overDueSuperviseEntities = gpmProjectSuperviseService.queryByWhere(sql.toString());
            if (CollectionUtils.isEmpty(overDueSuperviseEntities)) {
                return ReturnT.FAIL;
            }
            List<BasicEntity> resultList = new ArrayList<BasicEntity>();
            // 督办状态设置为超期
            for (GpmProjectSuperviseEntity overDueSuperviseEntity : overDueSuperviseEntities) {
                overDueSuperviseEntity.setSupervisestate("1");
                resultList.add(overDueSuperviseEntity);
            }
            gpmProjectSuperviseService.updateBatch(resultList);
            return ReturnT.SUCCESS;
        } catch (Exception ex) {
            throw new MessageException(ex);
        }
    }
}
